package com.xudow.app.newui;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ListView;
import android.widget.TextView;
import com.activeshare.edu.ucenter.models.base.Agencies;
import com.xudow.app.R;
import com.xudow.app.newui.base.BaseUiActivity;
import com.xudow.app.ui.task.GetAgencyListTask;
import java.util.List;
import butterknife.BindView;

public class AgencyListActivity extends BaseUiActivity {

    @BindView(R.id.agency_list)
    ListView agency_list;

    private boolean gohome=false;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_agency_list);
        gohome=getIntent().getBooleanExtra("GOTOHOME",false);

    }

    public void onBackBtnClick(View view) {
        finish();
    }

    @Override
    protected void initData() {
        GetAgencyListTask task=new GetAgencyListTask(this,handler);
        addTask(task);
        task.execute();
        showLodingDialog("正在加载");
    }
    private List<Agencies> data;
    private Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            hideLoadingDialog();
            if (msg.what==GetAgencyListTask.SUCCESS){
                data=(List<Agencies>)msg.obj;
                if (null!=data&&data.size()>0){
                    agency_list.setAdapter(new Myadapter());
                }
            }else
                getMyApp().makeToast(msg.getData().getString("errMessage"));
        }
    };
    @Override
    protected void initListener() {
        agency_list.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            @Override
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                Intent intent=new Intent(AgencyListActivity.this,AgencyLoginActivity.class);
                intent.putExtra("title",data.get(position).getName());
                intent.putExtra("agencyId",data.get(position).getId()+"");

                startActivity(intent);
            }
        });
    }

    class Myadapter extends BaseAdapter{
        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            TextView tv;
            if (null==convertView){
                convertView=View.inflate(AgencyListActivity.this,R.layout.item_agency_list,null);
                tv=(TextView)convertView.findViewById(R.id.name_tv);
                convertView.setTag(tv);
            }else
                tv=(TextView)convertView.getTag();
                tv.setText(data.get(position).getName());
            return convertView;
        }
    }
}
